<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(C卷,200分)- 田忌赛马（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给定两个只包含数字的数组a&#xff0c;b&#xff0c;调整数组 a 里面的数字的顺序&#xff0c;使得尽可能多的a[i] &gt; b[i]。</p> 
<p>数组a和b中的数字各不相同。</p> 
<p>输出所有可以达到最优结果的a数组的结果。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入的第一行是数组 a 中的数字&#xff0c;其中只包含数字&#xff0c;每两个数字之间相隔一个空格&#xff0c;a数组大小不超过10。</p> 
<p>输入的第二行是数组 b 中的数字&#xff0c;其中只包含数字&#xff0c;每两个数字之间相隔一个空格&#xff0c;b数组大小不超过10。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出所有可以达到最优结果的 a 数组的数量。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">11 8 20<br /> 10 13 7</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">1</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">最优结果只有一个&#xff0c;a &#61; [11, 20, 8]&#xff0c;故输出1</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">11 12 20<br /> 10 13 7</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">2</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">有两个a数组的排列可以达到最优结果&#xff0c;[12, 20, 11] 和 [11, 20, 12]&#xff0c;故输出2</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">1 2 3<br /> 4 5 6</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">6</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">a无论如何都会全输&#xff0c;故a任意排列都行&#xff0c;输出所有a数组的排列&#xff0c;6种排法。</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题数量级不大&#xff0c;可以考虑暴力破解。即求解a数组的所有全排列&#xff0c;然后将a数组的每个全排列和b数组逐个元素进行比较&#xff0c;统计每个全排列中a[i] &gt; b[i]的个数biggerCount。我们保留最大的biggerCount 为 maxBiggerCount。</p> 
<p>最终统计的是a[i] &gt; b[i]的个数为maxBiggerCount的全排列的数量。</p> 
<p></p> 
<p>关于全排列的求解&#xff0c;可以参考&#xff1a;</p> 
<p><a href="https://blog.csdn.net/qfc_128220/article/details/127286788?csdn_share_tail&#61;%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22127286788%22%2C%22source%22%3A%22qfc_128220%22%7D" title="LeetCode - 46 全排列_全排列 46 力扣-CSDN博客">LeetCode - 46 全排列_全排列 46 力扣-CSDN博客</a></p> 
<p></p> 
<p>本题不需要我们输出具体排列&#xff0c;因此不用定义path记录全排列。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">const rl &#61; require(&#34;readline&#34;).createInterface({ input: process.stdin });
var iter &#61; rl[Symbol.asyncIterator]();
const readline &#61; async () &#61;&gt; (await iter.next()).value;

void (async function () {
  const a &#61; (await readline()).split(&#34; &#34;).map(Number);
  const b &#61; (await readline()).split(&#34; &#34;).map(Number);

  let maxBiggerCount &#61; 0;
  let ans &#61; 0;

  function dfs(level, used, biggerCount) {
    if (level &gt;&#61; a.length) {
      if (biggerCount &gt; maxBiggerCount) {
        maxBiggerCount &#61; biggerCount;
        ans &#61; 1;
      } else if (biggerCount &#61;&#61; maxBiggerCount) {
        ans &#43;&#61; 1;
      }
    }

    for (let i &#61; 0; i &lt; a.length; i&#43;&#43;) {
      if (used[i]) continue;

      used[i] &#61; true;
      // biggerCount记录当前全排列中a[level] &gt; b[level]的位置的数量, 此时a[level] &#61;&#61; a[i]
      dfs(level &#43; 1, used, biggerCount &#43; (a[i] &gt; b[level] ? 1 : 0));
      used[i] &#61; false;
    }
  }

  dfs(0, new Array(a.length).fill(false), 0);

  console.log(ans);
})();
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  static int[] a;
  static int[] b;

  static int maxBiggerCount &#61; 0;
  static int ans &#61; 0;

  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    a &#61; Arrays.stream(sc.nextLine().split(&#34; &#34;)).mapToInt(Integer::parseInt).toArray();
    b &#61; Arrays.stream(sc.nextLine().split(&#34; &#34;)).mapToInt(Integer::parseInt).toArray();

    // 求解a的全排列
    dfs(0, new boolean[a.length], 0);

    System.out.println(ans);
  }

  public static void dfs(int level, boolean[] used, int biggerCount) {
    if (level &gt;&#61; a.length) {
      if (biggerCount &gt; maxBiggerCount) {
        maxBiggerCount &#61; biggerCount;
        ans &#61; 1;
      } else if (biggerCount &#61;&#61; maxBiggerCount) {
        ans&#43;&#43;;
      }

      return;
    }

    for (int i &#61; 0; i &lt; a.length; i&#43;&#43;) {
      if (used[i]) continue;

      used[i] &#61; true;
      // biggerCount记录当前全排列中a[level] &gt; b[level]的位置的数量, 此时a[level] &#61;&#61; a[i]
      dfs(level &#43; 1, used, biggerCount &#43; (a[i] &gt; b[level] ? 1 : 0));
      used[i] &#61; false;
    }
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
a &#61; list(map(int, input().split()))
b &#61; list(map(int, input().split()))

maxBiggerCount &#61; 0
ans &#61; 0


# 算法入口
def dfs(level, used, biggerCount):
    global maxBiggerCount, ans

    if level &gt;&#61; len(a):
        if biggerCount &gt; maxBiggerCount:
            maxBiggerCount &#61; biggerCount
            ans &#61; 1
        elif biggerCount &#61;&#61; maxBiggerCount:
            ans &#43;&#61; 1

        return

    for i in range(len(a)):
        if used[i]:
            continue

        used[i] &#61; True
        # biggerCount记录当前全排列中a[level] &gt; b[level]的位置的数量, 此时a[level] &#61;&#61; a[i]
        dfs(level &#43; 1, used, biggerCount &#43; (1 if a[i] &gt; b[level] else 0))
        used[i] &#61; False


# 算法调用
dfs(0, [False] * len(a), 0)

print(ans)
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

#define MAX_SIZE 10

int a[MAX_SIZE];
int a_size &#61; 0;

int b[MAX_SIZE];
int b_size &#61; 0;

int maxBiggerCount &#61; 0;
int ans &#61; 0;

void dfs(int level, int used[], int biggerCount) {
    if (level &gt;&#61; a_size) {
        if (biggerCount &gt; maxBiggerCount) {
            maxBiggerCount &#61; biggerCount;
            ans &#61; 1;
        } else if (biggerCount &#61;&#61; maxBiggerCount) {
            ans &#43;&#61; 1;
        }

        return;
    }

    for (int i &#61; 0; i &lt; a_size; i&#43;&#43;) {
        if (used[i]) continue;

        used[i] &#61; 1;
        // biggerCount记录当前全排列中a[level] &gt; b[level]的位置的数量, 此时a[level] &#61;&#61; a[i]
        dfs(level &#43; 1, used, biggerCount &#43; (a[i] &gt; b[level] ? 1 : 0));
        used[i] &#61; 0;
    }
}

int main() {
    while (scanf(&#34;%d&#34;, &amp;a[a_size&#43;&#43;])) {
        if (getchar() !&#61; &#39; &#39;) break;
    }

    while (scanf(&#34;%d&#34;, &amp;b[b_size&#43;&#43;])) {
        if (getchar() !&#61; &#39; &#39;) break;
    }

    int used[MAX_SIZE] &#61; {0};

    // 求解a的全排列
    dfs(0, used, 0);

    printf(&#34;%d\n&#34;, ans);

    return 0;
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>